﻿namespace WriterStars.Models.ViewModels
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using WriterStars.Model;

    public class ContestViewModel
    {
        public ContestViewModel()
        {
        }

        public ContestViewModel(Contest contest, string currentUserId = "", IEnumerable<Like> currentUserBookLikes = null)
            : this()
        {
            this.Id = contest.Id;
            this.Name = contest.Name;
            this.Regulations = contest.Regulations;
            this.Regulations = contest.Regulations;

            if (!string.IsNullOrEmpty(currentUserId))
            {

                var warsBookViewModel = contest.ContestBooks.Select(x => new ContestBookViewModel(x, currentUserBookLikes.FirstOrDefault(y => y.UserId == currentUserId && y.BookId == x.BookId))).ToList();
                
                this.ContestBooks = new List<ContestBookViewModel>(warsBookViewModel);
            }
            else
            {
                var warsBookViewModel = contest.ContestBooks.Select(x => new ContestBookViewModel(x)).ToList();
                this.ContestBooks = new List<ContestBookViewModel>(warsBookViewModel);
            }

            this.StartDate = contest.StartDate;
            this.EndDate = contest.EndDate;
        }

        public int Id { get; set; }

        public string Name { get; set; }

        public string Regulations { get; set; }

        public List<ContestBookViewModel> ContestBooks { get; set; }

        public DateTime StartDate { get; set; }

        public DateTime EndDate { get; set; }

        public ContestState State
        {
            get
            {
                if (this.StartDate >= DateTime.Now) return ContestState.Upcoming;
                if (this.StartDate < DateTime.Now && DateTime.Now < this.EndDate) return ContestState.Active;
                if (this.EndDate <= DateTime.Now) return ContestState.Finished;

                throw new ArgumentException("The Dates of the contest are not correct");
            }
        }

        public string Color
        {
            get
            {
                switch (this.State)
                {
                    case ContestState.Upcoming:
                        return "BE2C37";
                    case ContestState.Active:
                        return "FFCE00";
                    case ContestState.Finished:
                        return "91BE2C";
                    default:
                        throw new ArgumentException("State not suported");
                }
            }
        }
    }
}